package com.qf.electronic.mall.sms.service.handler;

import com.qf.electronic.mall.sms.service.util.SmsUtil;
import com.qf.constant.RabbitmqConstant;
import com.qf.constant.RedisConstant;
import com.qf.util.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 作者：张健
 * 项目名：electronic-mall
 * 时间：2020/3/10  22:08
 * 描述：
 */
@Component
public class SMSHandler {

    @Value("${sms.account}")
    private String account;

    @Value("${sms.password}")
    private String password;

    @Value("${sms.veryCode}")
    private String veryCode;

    @Value("${sms.http_url}")
    private  String http_url;

    @Value("${sms.tplId}")
    private String tplId;

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues = RabbitmqConstant.SMS_SEND_QUEUE)
    public void process(String phone){
        //1.发送短信
        String code=String.valueOf((int)((Math.random()*9+1)*1000));
        String content="@1@="+code;
        SmsUtil.account=account;
        SmsUtil.password=password;
        SmsUtil.veryCode=veryCode;
        SmsUtil.http_url=http_url;
        SmsUtil.sendTplSms(phone,tplId,content);

        //将手机号存入Redis中
        String redisKey = StringUtils.getRedisKey(RedisConstant.REGISTER_PHONE, phone);

        redisTemplate.opsForValue().set(redisKey,code,1, TimeUnit.MINUTES);

    }
}
